package com.ruoyi.web.controller.moto;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.MotoInfo;
import com.ruoyi.system.service.IMotoInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 摩托基础信息Controller
 *
 * @author ruoyi
 * @date 2024-08-12
 */
@RestController
@RequestMapping("/moto/info")
public class MotoInfoController extends BaseController {
    @Autowired
    private IMotoInfoService motoInfoService;

    /**
     * 查询摩托基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('moto:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(MotoInfo motoInfo) {
        startPage();
        List<MotoInfo> list = motoInfoService.selectMotoInfoList(motoInfo);
        return getDataTable(list);
    }

    /**
     * 导出摩托基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('moto:info:export')")
    @Log(title = "摩托基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MotoInfo motoInfo) {
        List<MotoInfo> list = motoInfoService.selectMotoInfoList(motoInfo);
        ExcelUtil<MotoInfo> util = new ExcelUtil<MotoInfo>(MotoInfo.class);
        util.exportExcel(response, list, "摩托基础信息数据");
    }

    /**
     * 获取摩托基础信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('moto:info:query')")
    @GetMapping(value = "/{infoId}")
    public AjaxResult getInfo(@PathVariable("infoId") Long infoId) {
        return success(motoInfoService.selectMotoInfoByInfoId(infoId));
    }

    /**
     * 新增摩托基础信息
     */
    @PreAuthorize("@ss.hasPermi('moto:info:add')")
    @Log(title = "摩托基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MotoInfo motoInfo) {
        return toAjax(motoInfoService.insertMotoInfo(motoInfo));
    }

    /**
     * 修改摩托基础信息
     */
    @PreAuthorize("@ss.hasPermi('moto:info:edit')")
    @Log(title = "摩托基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MotoInfo motoInfo) {
        return toAjax(motoInfoService.updateMotoInfo(motoInfo));
    }

    /**
     * 删除摩托基础信息
     */
    @PreAuthorize("@ss.hasPermi('moto:info:remove')")
    @Log(title = "摩托基础信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable Long[] infoIds) {
        return toAjax(motoInfoService.deleteMotoInfoByInfoIds(infoIds));
    }

    @Log(title = "摩托车数据导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('moto:info:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<MotoInfo> util = new ExcelUtil<>(MotoInfo.class);
        List<MotoInfo> motoList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = motoInfoService.importUser(motoList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<MotoInfo> util = new ExcelUtil<>(MotoInfo.class);
        util.importTemplateExcel(response, "摩托车数据");
    }

}
